$(function() {
	var _this = '';
	var flag = 0;
	var n = [];
	var uploadPath = $("#ctx").val() + "/resources/upload/";
	$("#attach").uploadify(
			{
				swf : $("#ctx").val() + "/resources/uploadify/uploadify.swf",
				uploader:$("#ctx").val()+"/admin/topic/upload.do",
				fileTypeExts : "*.rtmp;*.flv;*.mp4;*.f4v;*.m3u3",
				formData : {
					"sid" : $("#sid").val(),
					"attTypeId" : String($('.videoId').val())
				},
				fileObjName : "attach",
				auto : false,
				onUploadSuccess : function(file, data, response) {
					var ao = $.parseJSON(data);
					var suc = $.ajaxCheck(ao);
					if (suc) {
						var node = createAttachNode(ao.obj);
						$("#tbody").append(node);
						UE.getEditor(ao.obj.id + 'm', {
							toolbars : [ [ 'fullscreen', 'undo', 'redo',
									'bold', 'fontsize', 'justifyleft',
									'justifyright', 'justifycenter',
									'justifyjustify', 'forecolor',
									'simpleupload' ] ],
							autoHeightEnabled : false,
							autoFloatEnabled : true
						});
						upVideoImg(ao.obj.id);
					}
				}
			});

	function upVideoImg(videoImgId) {
		$('#' + videoImgId + 'vi').uploadify(
				{
					swf : $("#ctx").val()
							+ "/resources/uploadify/uploadify.swf",
					uploader:$("#ctx").val()+"/admin/topic/upload.do",
					queueID : '000',// 官方是通过这个Id来存文件列表的,随便赴个值给他就取消进度条了
					fileTypeExts : "*.jpg;*.png",
					formData : {
						"sid" : $("#sid").val(),
						"videoId" : String(videoImgId),
						"attTypeId" : String($('.videoId').val())
					},
					fileObjName : "attach",
					auto : true,
					onUploadSuccess : function(file, data, response) {
						var vi = $.parseJSON(data);
						var suc = $.ajaxCheck(vi);
						if (suc) {
							var node = "<div id='aaa' class='"+vi.obj.id+"dd'><img src='" + uploadPath + "thumbnail/"
									+ vi.obj.newName + "'/>"
							node+="<div class='bfont' id='del-VideoImg' delId='"+vi.obj.id+"' parId='"+videoImgId+"'>删除</div></div>";
							$('#' + videoImgId + 'vi').remove();
							$('#' + videoImgId + 'div').append(node);
						}
					}
				});

	}
	$("#ok_attach").on("click", "#del-VideoImg", function() {
		_this = this;
		$('#mes').html('确定要删除吗?');
		$('.con-mes').fadeIn();
		$('.cover').css('display', 'block');
	});
	
	function createAttachNode(attach) {
		n.push(attach.newName);
		var node = "<tr>";
		if (attach.isImg) {
			node += "<td><img src='" + uploadPath + "thumbnail/"
					+ attach.newName + "'/></td>";
		} else if (attach.isVideo) {
			node += "<td><div id='" + attach.id
					+ "div'><input type='file' name='attach' id='" + attach.id
					+ "vi' /></div></td>"
		} else {
			node += "<td>普通类型附件</td>";
		}
		node += "<td style='width:100px;'>"
				+ $('.videoId option:selected').text() + "</td>"
		node += "<td><textarea id='"
				+ attach.id
				+ "n' class='proNameText'>"+attach.oldName+"</textarea></td>"
		node += "<td style='border: 1px solid black; '><textarea id='"
				+ attach.id
				+ "m' style='width:700px;height:200px;'></textarea></td>";
		node += "<td><span id='"
				+ attach.id
				+ "s' style='color:red;'>未保存</span><div class='confirm bfont' proName='"
				+ attach.id + "n' proMessage='" + attach.id + "m' id='"
				+ attach.id + "' isSave='" + attach.id + "s'>保存</div>"
				+ "<div proName='" + attach.id + "n' proMessage='" + attach.id
				+ "m' class='edit bfont' notSave='" + attach.id + "s'>修改</div>"
				+ "<div class='bfont delete' id='" + attach.id
				+ "'>删除</div></td>"
		if (attach.isImg) {
		} else {
		}
		node += "</tr>";
		return node;
	}
	$("#ok_attach").on("click", ".delete", function() {
		_this = this;
		$('#mes').html('确定要删除吗?');
		$('.con-mes').fadeIn();
		$('.cover').css('display', 'block');
		
	});
	$('#con-mes').click(function() {
		if (flag == 0) {
			var id = $(_this).attr('id');
			if(id=='del-VideoImg')
				{
				var delId=$(_this).attr('delId');
				var parId=$(_this).attr('parId');
				var node="<input type='file' name='attach' id='" + parId
					+ "vi' />";
				dwrService.deleteAttach(delId);
				$('.'+delId+'dd').empty();
				$('#'+parId+'div').append(node);
				upVideoImg(parId);
				}else{
					dwrService.deleteAttach(id);
					$(_this).parent("td").parent("tr").empty();
				}
			$('.con-mes').fadeOut();
			$('.cover').css('display', 'none');
			_this = '';
		} else {
			window.opener.location.reload(true);
			window.close();
		}
	});
	$('#del-mes').click(function() {
		$('.con-mes').fadeOut();
		$('.cover').css('display', 'none');
		flag = 0;
	});
	$('.complete').click(function() {
		$('#mes').html('该操作不可逆，请确定保存完毕~');
		flag = 1;
		$('.con-mes').fadeIn();
		$('.cover').css('display', 'block');
	});
	$("#ok_attach").on("click", ".edit", function() {
		UE.getEditor($(this).attr('proMessage')).setEnabled();
		$('#' + $(this).attr('proName')).attr('disabled', false);
		$('#' + $(this).attr('notSave')).html('未保存').css('color', 'red');
	});
	$("#ok_attach").on("click", ".confirm", function() {
		var proName = $('#' + $(this).attr('proName')).val();
		var id = $(this).attr('id');
		var proMessage = UE.getEditor($(this).attr('proMessage')).getContent();
		dwrService.updatePro(proName, proMessage,null, id);
		UE.getEditor($(this).attr('proMessage')).setDisabled('fullscreen');
		$('#' + $(this).attr('proName')).attr('disabled', true);
		$('#' + $(this).attr('isSave')).html('已保存').css('color', 'green');
	});
	$("#upload").click(function() {
		$("#attach").uploadify("upload", "*");
	});
	$(".videoId")
			.change(
					function() {
						$("#attach")
								.uploadify(
										{
											swf : $("#ctx").val()
													+ "/resources/uploadify/uploadify.swf",
											uploader:$("#ctx").val()+"/admin/topic/upload.do",
											fileTypeExts : "*.rtmp;*.flv;*.mp4;*.f4v;*.m3u3",
											formData : {
												"sid" : $("#sid").val(),
												"attTypeId" : String($(
														'.videoId').val())
											},
											fileObjName : "attach",
											auto : false,
											onUploadSuccess : function(file,
													data, response) {
												var ao = $.parseJSON(data);
												var suc = $.ajaxCheck(ao);
												if (suc) {
													var node = createAttachNode(ao.obj);
													$("#tbody").append(node);
													UE
															.getEditor(
																	ao.obj.id
																			+ 'm',
																	{
																		toolbars : [ [
																				'fullscreen',
																				'undo',
																				'redo',
																				'bold',
																				'fontsize',
																				'justifyleft',
																				'justifyright',
																				'justifycenter',
																				'justifyjustify',
																				'forecolor',
																				'simpleupload' ] ],
																		autoHeightEnabled : false,
																		autoFloatEnabled : true
																	});
													upVideoImg(ao.obj.id);

												}
											}
										});
					});
})